﻿package boy.greensock
{
   import com.greensock.TweenLite;
   import com.greensock.plugins.TweenPlugin;
   
   import flash.text.TextField;

   /**
    * DecoderTextPlugin tweens the characters of a TextField giving the appearance of text decoding.
    *
    * VERSION: 1.2
    * DATE:3/27/2010
    *
    * @author Jack Doyle, jack@greensock.com
    * @link http://designmarco.com
    *
    * @usage
    * import com.greensock.TweenMax;
    * import com.greensock.plugins.TweenPlugin;
    * import boy.greensock.DecoderTextPlugin;
    *
    * TweenPlugin.activate([DecoderTextPlugin]);
    * TweenMax.to(tf, 0.5, { decoder:"Lorem Ipsum" } );
    */
   public class DecoderTextPlugin extends TweenPlugin
   {
      public static const API:Number = 1.0;
      protected var target:TextField;
      protected var tween:TweenLite;
      protected var prevTime:Number = 0;
      protected var oldText:String;
      protected var oldLength:int;
      protected var newText:String;
      protected var newLength:int;

      public function DecoderTextPlugin()
      {
         this.propName = "decoder";
         this.overwriteProps = [];
      }

      override public function onInitTween(target:Object, value:*, tween:TweenLite):Boolean
      {
         if (!(target is TextField)) return false;

         this.target = target as TextField;
         this.tween = tween;

         oldText = target.text;
         oldLength = oldText.length + 1;

         newText = String(value);
         newLength = newText.length + 1;

         return true;
      }

      override public function set changeFactor(n:Number):void
      {
         var counter:int, valueA:String, valueB:String;

         if (tween.cachedTime > prevTime)
         {
            counter = int(newLength * n - 1 + 0.5);
            valueA = newText.substr(0, counter);
            valueB = newText.substr(counter);
         }
         else
         {
            counter = int(oldLength * (1 - n) - 1 + 0.5);
            valueA = oldText.substr(0, counter);
            valueB = oldText.substr(counter);
         }

         var decoder:String = "";
         var i:int = valueB.length;
         while (i--)
         {
            decoder += String.fromCharCode(((Math.random() * 26) + 65) >> 0);
         }

         target.text = valueA + decoder;
         prevTime = tween.cachedTime;
      }

   }

}